package com.sh.test;

import com.sh.promise.IPromise;
import com.sh.promise.Promise;
import com.sh.promise.then.OnCompleteListener;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

/** 测试设置成功结果
 * @Date 13:49 2023/3/9
 **/
public class Demo05 {
    public static void main(String[] args) {
        IPromise promise = new Promise.Builder().promiseHandler(executor -> {
            System.out.println("ssss");
            //throw new RuntimeException("访问出现异常");
            return "hahah";
        }).build();

        //promise.setFailure(new RuntimeException("213231"));
        promise.setSuccess("12312");
        System.out.println(promise.get());

    }
    public static void test(){
        List<IPromise> list = new ArrayList<>();
        ExecutorService pool = Promise.pool(3, 10);
        for (int i = 0; i < 1000; i++) {
            list.add(new Promise.Builder().pool(pool).promiseHandler(executor -> {
                //throw new RuntimeException("访问出现异常");
                return Thread.currentThread().getName() + "hahah";
            }).build());
        }
        for (IPromise iPromise : list) {
            iPromise.setSuccess(Thread.currentThread().getName() +"122");
            System.out.println(iPromise.get());
            iPromise.addListener((resolvedData, e) -> System.out.println("resolvedData = " + resolvedData));
        }
        pool.shutdown();
    }
}
